﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyDataService;
using QuanLyBanHang.DAL;
namespace QuanLyBanHang
{
    public partial class frmTonKho : Form
    {
        public frmTonKho()
        {
            InitializeComponent();
            dataservice.strConn = ConfigurationManager.ConnectionStrings["strConn"].ConnectionString;
        }

        private string MaLH = "";
        private string MaNCC="";
        private string MaKho="";
        private int TruocHan;
        private string NgayTuongLai;
        private bool FormLoad = true;
        DalHangHetHan hetHan =new DalHangHetHan();
        private void cboLoaiHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (FormLoad==false)
            {
                MaLH = cboLoaiHang.SelectedValue.ToString();
                LocThongTin();
            }
        }

        private void cboKhoHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (FormLoad == false)
            {
                MaKho = cboKhoHang.SelectedValue.ToString();
                LocThongTin();
            }
        }

        private void cboNhaCungCap_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (FormLoad == false)
            {
                MaNCC = cboNhaCungCap.SelectedValue.ToString();
                LocThongTin();
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (FormLoad == false)
            {
                TruocHan =int.Parse( cboTruocNgayHetHan.SelectedValue.ToString());
                LocThongTin();
            }
        }

        private void dtpEnd_ValueChanged(object sender, EventArgs e)
        {
            if (FormLoad == false)
            {
                NgayTuongLai = dtpEnd.Value.ToString("MM/dd/yyyy");
                LocThongTin();
            }
        }

        private void frmTonKho_Load(object sender, EventArgs e)
        {
            hetHan.loadcbo(cboTruocNgayHetHan,cboNhaCungCap,cboKhoHang,cboLoaiHang);
            FormLoad = false;
        }
        void LocThongTin()
        {
            dgvDanhSachHang.DataSource = hetHan.LocHangHoa(MaLH, MaNCC, MaKho, TruocHan, NgayTuongLai);
        }

        private void btnXoaToanBo_Click(object sender, EventArgs e)
        {

        }

        private void btnXoaHang_Click(object sender, EventArgs e)
        {
            xoahang();
        }
        void xoahang()
        {
            foreach (DataGridViewRow row in dgvDanhSachHang.SelectedRows)
            {
                hetHan.XoaHang(row.Cells[0].Value.ToString());
                dgvDanhSachHang.DataSource = hetHan.LocHangHoa(MaLH, MaNCC, MaKho, TruocHan, NgayTuongLai);
            }
        }

        private void contextMenuStripGrid_Opening(object sender, CancelEventArgs e)
        {
            if (dgvDanhSachHang.SelectedRows.Count==0|dgvDanhSachHang.Rows.Count==0)
            {
                contextMenuStripGrid.Enabled = false;
            }
            else
            {
                contextMenuStripGrid.Enabled = true;
            }
        }

        private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
           
        }
    }
}
